<?php
namespace app\common\model;


use think\Model;

class User extends Model
{
    protected $pk = 'id';
    protected $table = 'qi_user';

    protected $autoWriteTimestamp = true;
    protected $createTime = 'create_time';
    protected $updateTime = 'update_time';

    protected $dateFormat = 'Y-m-d H:i:s';


    public function getInfoById($id)
    {
        $info = User::where('id',$id)->find();
        return $info;
    }

    public function getInfoByEmail($email)
    {
        $info = User::where('email',$email)->find();
        return $info;
    }

    /*
     * 添加用户
     */
    public function addUser($info)
    {
        $info['password'] = md5($info['password']);
        $result = User::create($info);
        return $result;
    }

    /*
     * 更新用户
     */
    public function editUser($info)
    {
        $result = User::update($info);
        return $result;
    }

    /*
     * 更新头像
     */
    public function changeAvatar($data)
    {
        $result = User::update($data);
        return $result;
    }

    /*
     * 删除id对应的图片
     */
    public function delAvatar($id)
    {
        $avatar = User::where('id',$id)->value('avatar');

        if (!empty($avatar)) {
            $fileName = $_SERVER['DOCUMENT_ROOT'] . '/uploads/avatar/' . $avatar;

            if (file_exists($fileName)) {
                unlink($fileName);
            }
        };
    }

}